#
#    RubyIZUMI
#
#    Copyright (C) 2008 Yaacov Akiba Slama <ya@slamail.org>
#    Copyright (C) 2008 Takuma Mori, SGRA Corporation
#    <mori@sgra.co.jp> <http://www.sgra.co.jp/en/>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

HandshakeServer = [
0x01,0x86,0x4f,0x7f,0x00,0x00,0x00,0x00,0x6b,0x04,0x67,0x52,0xa2,0x70,0x5b,0x51,
0xa2,0x89,0xca,0xcc,0x8e,0x70,0xf0,0x06,0x70,0x0e,0xd7,0xb3,0x73,0x7f,0x07,0xc1,
0x72,0xd6,0xcb,0x4c,0xc0,0x45,0x0f,0xf5,0x4f,0xec,0xd0,0x2f,0x46,0x2b,0x76,0x10,
0x92,0x1b,0x0e,0xb6,0xed,0x71,0x73,0x45,0xc1,0xc6,0x26,0x0c,0x69,0x59,0x7b,0xbb,
0x53,0xb9,0x10,0x4d,0xea,0xc1,0xe7,0x7b,0x70,0xde,0xdc,0xf8,0x84,0x90,0xbf,0x80,
0xe8,0x85,0xb2,0x46,0x2c,0x78,0xa1,0x85,0x01,0x8f,0x8b,0x05,0x3f,0xa1,0x0c,0x1a,
0x78,0x70,0x8c,0x8e,0x77,0x67,0xbc,0x19,0x2f,0xab,0x26,0xa1,0x7e,0x88,0xd8,0xce,
0x24,0x63,0x21,0x75,0x3a,0x5a,0x6f,0xc2,0xa1,0x2d,0x4f,0x64,0xb7,0x7b,0xf7,0xef,
0xda,0x45,0xb2,0x51,0xfd,0xcb,0x74,0x49,0xfd,0x63,0x8b,0x88,0xfb,0xde,0x5a,0x3b,
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ef,0x59,0xe6,0xd6,0xdd,0x6a,0x74,0x82,0xb8,0xc5,0xfb,
0x1d,0xe8,0xfc,0x67,0x4f,0x4d,0xb5,0xcf,0xa9,0x52,0x94,0xc5,0xb7,0x32,0xa0,0x45,
0x0a,0x35,0x44,0x59,0x1e,0x1c,0x64,0x89,0x51,0x80,0x7b,0x1f,0x02,0x77,0x81,0xfa,
0xe9,0x26,0x4c,0x5f,0xe2,0x0d,0x05,0x55,0xee,0x71,0x71,0xfc,0x35,0x33,0x22,0x63,
0xf5,0x36,0x45,0xf6,0x2f,0xd0,0x13,0xb7,0x58,0x4f,0x35,0x19,0x59,0x0a,0xe5,0xf8,
0x8a,0x4c,0x59,0x32,0xbf,0xca,0xb0,0x06,0xc2,0x6c,0xa9,0x48,0x5b,0x4c,0x76,0x24,
0xae,0x9d,0x5b,0x7b,0x79,0x38,0x4e,0x9e,0x47,0x12,0x8a,0xc6,0xe0,0x04,0x37,0x72,
0xdd,0xaf,0x3d,0x0d,0x68,0x7e,0xd8,0x80,0x7b,0x07,0x23,0xce,0x40,0x4a,0xed,0x83,
0x55,0x56,0xfd,0xdb,0x95,0xb3,0x1c,0x33,0xf1,0x43,0xa8,0x0e,0x5e,0x67,0xd6,0x3a,
0xd0,0x89,0x5e,0x72,0x77,0x7f,0x10,0x3c,0xc4,0x7c,0x9a,0xa3,0x55,0xc5,0xd3,0x5b,
0x3a,0xae,0x12,0x0c,0x71,0x73,0xa0,0x58,0x90,0x54,0xa8,0x1c,0x31,0x20,0xdb,0xde,
0xdd,0x35,0xb1,0x09,0xa2,0xd0,0x6e,0x39,0x39,0xa5,0x0a,0x3d,0x8a,0x00,0x4b,0x95,
0x6f,0x8c,0x12,0x41,0xc6,0x46,0x10,0x5e,0x9d,0x50,0x85,0x0e,0x6b,0x81,0xa7,0x3b,
0x35,0xa6,0x38,0xf5,0xc2,0xba,0x6c,0x02,0xda,0x27,0x29,0x6e,0xe9,0x54,0x41,0xa4,
0x94,0x75,0xe8,0x55,0xc0,0xe3,0xc2,0x91,0x8a,0x1d,0xfb,0x2b,0xba,0x43,0xe7,0x45,
0x85,0xe8,0x13,0x07,0x1d,0x9c,0x37,0xa8,0xf3,0xca,0xf4,0x19,0x77,0xc4,0x65,0xd6,
0x18,0x3e,0x60,0x08,0x74,0x49,0xba,0xc8,0x86,0x37,0x8a,0x0f,0x79,0x91,0x53,0x20,
0x23,0x00,0xb9,0xc5,0x1b,0x01,0xdd,0x10,0x34,0x05,0x42,0xa0,0x64,0xab,0x4d,0x51,
0xf4,0x53,0x35,0x18,0xde,0x20,0x1f,0xaa,0xe2,0x40,0x0d,0x6d,0x77,0x36,0x1f,0xee,
0x3a,0x93,0xdb,0x1d,0xd6,0xa0,0x23,0xcc,0xe6,0xa8,0x44,0x8e,0xae,0x9c,0xd7,0x97,
0x6a,0x99,0xee,0x40,0x15,0xd5,0x5a,0x6d,0xf6,0x9c,0x2c,0x52,0xcd,0xfa,0xf4,0xc8,
0x02,0xee,0xf2,0x76,0x8b,0x49,0x6d,0x66,0x83,0x5f,0xbe,0x05,0x8e,0xf2,0x27,0x73,
0xdb,0x00,0xeb,0x9a,0xb4,0xbf,0x47,0x9a,0xbd,0xf1,0x4f,0x70,0xed,0x33,0xce,0x31,
0x9d,0x9f,0x95,0x80,0x9e,0x73,0x11,0x6c,0x03,0x7b,0x6e,0x62,0x9c,0xd0,0xaa,0xf6,
0x5d,0xe0,0xd8,0x96,0x94,0x46,0xd1,0x10,0x3c,0x1b,0x9d,0x40,0xdd,0xab,0xec,0x8a,
0x5b,0x1a,0xb6,0x19,0x57,0x99,0x09,0xe8,0xec,0x82,0xdc,0x06,0x39,0x86,0x25,0x3b,
0x67,0xb5,0x17,0xc5,0x6e,0x6e,0x1c,0x6c,0xea,0xbe,0xb8,0xdd,0x68,0xf8,0xf3,0x18,
0xf2,0x3c,0x99,0xdc,0xa9,0xd3,0xb2,0x7a,0x40,0x70,0x4b,0xc2,0xd2,0xa7,0xb3,0x42,
0x19,0xff,0x0b,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d,0x76,0x69,0x89,0xf3,0x1e,0x62,0xf6,0x27,0x3d,0x3e,0x41,0x0f,0xf5,0x0f,0xc7,
0xf3,0x0e,0x3b,0xd5,0xed,0xcf,0xef,0x58,0xfa,0x39,0xdf,0x75,0x85,0x2b,0x8b,0xaa,
0x08,0x72,0x52,0xa7,0x98,0x42,0x95,0x7b,0xb7,0xe7,0x10,0xfe,0xdb,0x54,0x34,0xfb,
0x91,0x24,0x1c,0x07,0xfb,0x9c,0xce,0xd0,0x46,0xcf,0xc4,0x9d,0x09,0x49,0x24,0xec,
].pack('C*')

KeyServer = [
0x47,0x65,0x6e,0x75,0x69,0x6e,0x65,0x20,0x41,0x64,0x6f,0x62,0x65,0x20,0x46,0x6c,
0x61,0x73,0x68,0x20,0x4d,0x65,0x64,0x69,0x61,0x20,0x53,0x65,0x72,0x76,0x65,0x72,
0x20,0x30,0x30,0x31,0xf0,0xee,0xc2,0x4a,0x80,0x68,0xbe,0xe8,0x2e,0x00,0xd0,0xd1,
0x02,0x9e,0x7e,0x57,0x6e,0xec,0x5d,0x2d,0x29,0x80,0x6f,0xab,0x93,0xb8,0xe6,0x36,
0xcf,0xeb,0x31,0xae,
].pack('C*')

require 'hmac-sha2'

module RTMP
  class Handshake
	def Handshake.get_handshake(client)
		client_a = client.unpack('C*')
		if (client_a[4] ==  0)
			return client
		else
			part = client_a[(client_a[8] + client_a[9] + client_a[10] + client_a[11]) % 728 + 12,32].pack('C*')
			a = HMAC::SHA256.digest(KeyServer, part)
			server_a = Array.new()
			1504.times do
				server_a.push(rand(256))
			end
			server = server_a.pack("C*")
			server = server + HMAC::SHA256.digest(a, server)
			return server
		end
  	end
  end
end
